package com.zjdiepu.www.manage.api.yw;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.CreditPeopleType;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.commpent.ViewScopeControllerHelper;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessBuyerVoMapper;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.dh.IDhInsteadPaymentService;
import com.zjdiepu.www.ibiz.dh.IOverdueService;
import com.zjdiepu.www.ibiz.icbc.IYwOrdernoBankService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessBuyerService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.dh.dto.DhInsteadPayment;
import com.zjdiepu.www.model.dh.vo.YwCommonInfo;
import com.zjdiepu.www.model.dh.vo.YwOtherInfoVo;
import com.zjdiepu.www.model.icbc.dto.YwOrdernoBank;
import com.zjdiepu.www.model.icbc.ext.YwOrdernoBankExt;
import com.zjdiepu.www.model.yw.dto.YwBusinessBuyer;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoan;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;
import com.zjdiepu.www.util.ExceptionHelper;
@RestController
@RequestMapping(value= "YwBusinessCommonController", produces="application/json;charset=UTF-8")
@Api(tags = {"共用表头部分"}, produces="application/json;charset=UTF-8")
public class YwBusinessCommonController extends BaseController{
	@Resource
    private ViewScopeControllerHelper viewScopeControllerHelper;
	@Resource
    private ICachedDictionaryService cachedDictionaryService;
	@Resource
    private IYwBusinessService ywBusinessService;
	@Resource
	private IOverdueService overdueService;
	@Resource
	private IDhInsteadPaymentService dhInsteadPaymentService;
	@Resource
	private IYwBusinessBuyerService ywBusinessBuyerService;
	@Autowired
	private YwBusinessBuyerVoMapper ywBusinessBuyerVoMapper;
	@Resource
	private IYwOrdernoBankService ywOrdernoBankService;
	/**
	 * @param model
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "共用表头部分",response = Map.class, httpMethod="POST", tags={"查询"})
    @RequestMapping(value = "getCommonBusinessInfo", method = RequestMethod.POST)
	public Map<String,Object> getCommonBusinessInfo(Model model,String busiId){
		if (StringUtils.isBlank(busiId)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
		Map<String,Object> response=success();
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(busiId, model);
//    	update xiongxiaotun 2017-7-20
    	ywBusinessDetail.setDealerLevel(cachedDictionaryService.getName(ywBusinessDetail.getDealerLevel(), DataDicType.DEALER_LEVEL));
    	response.put("ywBusinessDetail", ywBusinessDetail);
    	YwBusinessCar car = viewScopeControllerHelper.preparedCar(busiId, null,model);
    	if(car != null) {
    		YwBusinessLoan ywBusinessLoan=viewScopeControllerHelper.preparedLoan(busiId, car.getId(), model,car.getCarType());
    		response.put("ywBusinessLoan", ywBusinessLoan);
    	}
    	if(ywBusinessDetail != null) {
    		String history = ywBusinessService.queryHistoryByCustomerNo(ywBusinessDetail.getCustomerNo());
        	response.put("historyBusiness", StringUtils.isBlank(history)?"-":(history.startsWith(",")?history.substring(1):history));
    	}
    	YwBusinessBuyer ywBusinessBuyer=new YwBusinessBuyer();
    	ywBusinessBuyer.setBusiId(busiId);
    	ywBusinessBuyer.setCarManType(CreditPeopleType.ZHU_JIE_REN.getCode());
    	ywBusinessBuyer=ywBusinessBuyerService.selectRefined(ywBusinessBuyer);
    	if(ywBusinessBuyer!=null){
    		ywBusinessBuyerVoMapper.mapper(ywBusinessBuyer);
    		response.put("ywBusinessBuyer", ywBusinessBuyer);
    	}
    	YwOrdernoBankExt ywOrdernoBankExt=new YwOrdernoBankExt();
    	ywOrdernoBankExt.setBusiId(busiId);
    	ywOrdernoBankExt.setDpStatus(DpStatus.NORMAL.getCode());
    	ywOrdernoBankExt.setRows(1);
    	ywOrdernoBankExt.setWhereSql(" AND BUSI_ID IS NOT NULL ");
    	List<YwOrdernoBank> list=ywOrdernoBankService.selectByExt(ywOrdernoBankExt);
    	if(list!=null && list.size()>0){
    		response.put("ywOrdernoBank", list.get(0));
    	}
    	return response;
	}
	/**
	 * @param model
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "共用表头共同购车人结清部分",response = Map.class, httpMethod="POST", tags={"查询"})
    @RequestMapping(value = "getCommonBusinessWithBuyersInfo", method = RequestMethod.POST)
	public Map<String,Object> getCommonBusinessWithBuyersInfo(Model model,String busiId){
		if (StringUtils.isBlank(busiId)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
		Map<String,Object> response=success();
    	YwCommonInfo ywInfo= overdueService.selectInfo(busiId);
    	if(ywInfo!=null){
    		response.put("ywInfo", ywInfo);
    	}
    	List<YwOtherInfoVo> listNew= new ArrayList<YwOtherInfoVo>();
    	List<YwOtherInfoVo> otherInfoList=overdueService.selectMulitList(busiId);
    	Double bAmount=0.0;
    	Double cAmount=0.0;
    	DhInsteadPayment dhInsteadPayment=null;
    	if(otherInfoList!=null && otherInfoList.size()>0){
    		for (YwOtherInfoVo ywOtherInfoVo : otherInfoList) {
    			dhInsteadPayment= new DhInsteadPayment();
    			dhInsteadPayment.setBusiId(ywOtherInfoVo.getBusiId());
    			dhInsteadPayment.setOrderSql(" CREATED DESC");
    			DhInsteadPayment insteadPayment=dhInsteadPaymentService.selectRefined(dhInsteadPayment);
    			if(insteadPayment!=null){
    				ywOtherInfoVo.setBalance(insteadPayment.getBalance());
    				ywOtherInfoVo.setDateNumOverdue(insteadPayment.getDateNumOverdue());
    			}
    			if(null !=ywOtherInfoVo.getCarManType()&&ywOtherInfoVo.getCarManType().equals(CreditPeopleType.ZHU_JIE_REN.getCode())){
    				if(ywOtherInfoVo.getbAmount()!=null){
    					bAmount+=ywOtherInfoVo.getbAmount();
    				}
    				if(ywOtherInfoVo.getcAmount()!=null){
    					cAmount+=ywOtherInfoVo.getcAmount();
    				}
    				ywOtherInfoVo.setbAmount(bAmount);
    				ywOtherInfoVo.setcAmount(cAmount);
    				ywOtherInfoVo.setContact1RelationDisplay(cachedDictionaryService.getName(ywOtherInfoVo.getContact1Relation(), DataDicType.CONTACT_RELATION));
    				ywOtherInfoVo.setContact2RelationDisplay(cachedDictionaryService.getName(ywOtherInfoVo.getContact2Relation(), DataDicType.CONTACT_RELATION));
    				response.put("buyers", ywOtherInfoVo);
    			}else{
    				for(int i=0;i<otherInfoList.size();i++){
    					YwOtherInfoVo sub = otherInfoList.get(i);
    	    			if(sub.getCarManType().equals(CreditPeopleType.PEI_OU.getCode())){
    	    				listNew.add(sub);
    	    				break;
    	    			}
    	    		}
    	    		
    	    		for(int i=0;i<otherInfoList.size();i++){
    	    			YwOtherInfoVo sub = otherInfoList.get(i);
    	    			if(sub.getCarManType().equals(CreditPeopleType.DAN_BAO_REN.getCode())){
    	    				listNew.add(sub);
    	    			}
    	    		}
    				response.put("otherBuyers", listNew);
    			}
			}
    	}
    	return response;
	}
}
